package com.bringway.p2p.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.CzDao;
import com.bringway.p2p.dao.YcxiDao;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:25:42
 * @功能：查询异常信息
 */

@Repository
public class YcxiDaoImpl  extends P2PJdbcDao implements YcxiDao{
	Logger logger = Logger.getLogger(YcxiDaoImpl.class);
	
	
	public List<Map<String, Object>> queryYcxiList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		if(StringUtils.isNotEmpty(startDate)){
			startDate=startDate+" 00:00:00";
		}
		if(StringUtils.isNotEmpty(endDate)){
			endDate=endDate+" 23:59:59";
		}
		
		String yhm=params.optString("yhm");

		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		
		
		paramMap.put("USER_NAME", yhm);
	
	
		sql.append(" select ID,USER_NAME,LOG_TYPE,LOG_CONTENT,date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') LOG_DATE from t_zhfw_sys_log ");
		sql.append(" where 1=1  ");
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') >= :startDate ");
			paramMap.put("startDate",startDate);
		}
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') <= :endDate ");
			paramMap.put("endDate",endDate);
		}
		
		if(StringUtils.isNotEmpty(yhm)){
			sql.append(" AND USER_NAME like "+ "'%"+yhm+"%'");
		}
		
	
		if(0 != limit){
			sql.append(" ORDER BY LOG_DATE DESC LIMIT :START,:LIMIT  ");
		}
		/*return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);
*/
		
		
		

		return this.getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new ResultSetExtractor<List<Map<String, Object>> >() {

			public List<Map<String, Object>>  extractData(ResultSet rs) throws SQLException, DataAccessException {
				 List<Map<String, Object>> list = new ArrayList<>();
				 while(rs.next()){
					 Map<String, Object> map = new HashMap<>();
					 map.put("ID", rs.getString("ID"));
					 map.put("USER_NAME", rs.getString("USER_NAME"));
					 map.put("LOG_TYPE", rs.getString("LOG_TYPE"));
					 map.put("LOG_CONTENT", rs.getString("LOG_CONTENT"));
					 map.put("LOG_DATE", rs.getString("LOG_DATE"));
					
					
					 String deviceOperType = rs.getString("LOG_TYPE");
					 String LOG_TYPEValue = "";
					 if("00".equals(deviceOperType)){
						 LOG_TYPEValue = "";
					 }else if("0".equals(deviceOperType)){
						 LOG_TYPEValue = "异常信息";
						 
					 }else if("1".equals(deviceOperType)){
						 LOG_TYPEValue = "债权截标";
						 
					 }else if("2".equals(deviceOperType)){
						 LOG_TYPEValue = "债权发放收益";
						 
					 }else if("3".equals(deviceOperType)){
						 LOG_TYPEValue = "募集期打款";
						 
					 }else if("4".equals(deviceOperType)){
						 LOG_TYPEValue = "单笔每日付收益";
						 
					 }else if("5".equals(deviceOperType)){
						 LOG_TYPEValue = "每日任务付收益还本";
						 
					 }else if("6".equals(deviceOperType)){
						 LOG_TYPEValue = "tzrxx表操作记录";
						 
					 }else if("7".equals(deviceOperType)){
						 LOG_TYPEValue = "散标投资每日付收益";
						 
					 }else if("8".equals(deviceOperType)){
						 LOG_TYPEValue ="散标投资定时任务";
						 
					 }else if("9".equals(deviceOperType)){
						 LOG_TYPEValue ="计划内募集期打款";
						 
					 }else if("10".equals(deviceOperType)){
						 LOG_TYPEValue ="计划内投资撤销退款";
						 
					 }else if("11".equals(deviceOperType)){
						 LOG_TYPEValue ="计划内项目截标";
						 
					 }else if("12".equals(deviceOperType)){
						 LOG_TYPEValue ="计划内项目到期定时器";
						 
					 }else if("13".equals(deviceOperType)){
						 LOG_TYPEValue ="计划内项目到期还本付息明细";
						 
					 }else if("14".equals(deviceOperType)){
						 LOG_TYPEValue ="今日收益提醒";
						 
					 }else if("15".equals(deviceOperType)){
						 LOG_TYPEValue ="转让专区项目截标";
						 
					 }else if("16".equals(deviceOperType)){
						 LOG_TYPEValue ="转让专区项目到期还本付息";
						 
					 }
					 map.put("LOG_TYPE_VALUE", LOG_TYPEValue);
					 list.add(map);
				 }
				return list;
			}
		});
	
		
	}

	@Override
	public int gettYcCount(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		
		String yhm = params.optString("yhm");
	
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("yhm", yhm);
		
		sql.append(" SELECT count(*)  ");
		sql.append(" FROM t_zhfw_sys_log ");
	
		sql.append("   where 1=1 ");
		if(StringUtils.isNotBlank(startDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') >= :startDate ");
			paramMap.put("startDate", startDate+" 00:00:00");
		}
		if(StringUtils.isNotBlank(endDate)){
			sql.append(" AND date_format(LOG_DATE,'%Y-%m-%d %H:%i:%s') <= :endDate ");
			paramMap.put("endDate", endDate+" 23:59:59");
		}
		if(StringUtils.isNotEmpty(yhm)){
			sql.append(" AND USER_NAME like "+ "'%"+yhm+"%'");
		}
	
		logger.info("gettjrCount sql is " + sql.toString());
		logger.info("gettjrCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}



}
